package io.gitee.thinkbungee.component.push.service.impl;

import cn.hutool.core.util.ArrayUtil;
import io.gitee.thinkbungee.component.push.service.MailPushService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author chenyichang
 * @since 2022/6/2
 */
@Slf4j
public class MailPushServiceImpl implements MailPushService {

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 发件人
     */
    private String from;

    public void setFrom(String from) {
        this.from = from;
    }

    @Override
    public void sendSimpleMail(String to, String subject, String content, String... cc) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        if (ArrayUtil.isNotEmpty(cc)) {
            message.setCc(cc);
        }
        try {
            mailSender.send(message);
            if (log.isInfoEnabled()) {
                log.info("简单文本邮件发送成功");
            }
        } catch (Exception e) {
            log.error("简单文本邮件发送失败", e);
        }
    }

    @Override
    public void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        if (ArrayUtil.isNotEmpty(cc)) {
            helper.setCc(cc);
        }
        try {
            mailSender.send(message);
            if (log.isInfoEnabled()) {
                log.info("html邮件发送成功");
            }
        } catch (Exception e) {
            log.error("html邮件发送失败", e);
        }
    }

    @Override
    public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        if (ArrayUtil.isNotEmpty(cc)) {
            helper.setCc(cc);
        }
        FileSystemResource fileSystemResource = new FileSystemResource(filePath);
        String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
        helper.addAttachment(fileName, fileSystemResource);

        try {
            mailSender.send(message);
            if (log.isInfoEnabled()) {
                log.info("带附件邮件发送成功");
            }
        } catch (Exception e) {
            log.error("带附件邮件发送失败", e);
        }
    }

    @Override
    public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId,
                                 String... cc) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        if (ArrayUtil.isNotEmpty(cc)) {
            helper.setCc(cc);
        }
        FileSystemResource resource = new FileSystemResource(new File(rscPath));
        helper.addInline(rscId, resource);

        try {
            mailSender.send(message);
            if (log.isInfoEnabled()) {
                log.info("嵌入静态资源的邮件发送成功");
            }
        } catch (Exception e) {
            log.error("嵌入静态资源的邮件发送失败", e);
        }
    }
}
